package com.agenda.server.recebedores;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import com.agenda.comum.ObjectRecebedorInformacao;
import com.agenda.controller.ChatController;
import com.agenda.model.RespostaRequisicao;
import com.agenda.model.Sala;
import com.agenda.model.UsuarioIP;
import com.agenda.server.Servidor;

public class CriarSalaChatRecebedor extends ObjectRecebedorInformacao {

	private RespostaRequisicao resposta;
	private ArrayList<UsuarioIP> usuariosIP = new ArrayList<UsuarioIP>();

	@Override
	public void receber(ObjectInputStream objectInput) throws Exception {
		Sala sala = (Sala) objectInput.readObject();
		System.out.println("Usuario que solicitou a sala - " + sala.getProprietario());
		try {
			ChatController.getINSTANCE().reservarSala(sala.getProprietario());

			usuariosIP = Servidor.getInstancia().retornarUsuariosIP(sala.getConvidados());

			resposta = new RespostaRequisicao(false, "");
		} catch (Exception e) {
			resposta = new RespostaRequisicao(true, e.getMessage());
		}
	}

	@Override
	protected RespostaRequisicao getReposta() {
		return resposta;
	}

	@Override
	protected void responder(OutputStream outputStream) throws Exception {
		ObjectOutputStream objOutput = new ObjectOutputStream(sck.getOutputStream());
		objOutput.writeObject(usuariosIP);
	}
}
